package com.zf.study.client;

import com.zf.study.message.HeartBeatMessage;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

public class ChatClient2 {


    public static String ip = "127.0.0.1";

    public static int port = 8088;

    public static void main(String[] args) throws IOException {
        Socket socket = new Socket(ip, port);
        PrintWriter mainWriter = new PrintWriter(socket.getOutputStream(), true);

        // 启动一个线程专门接收服务器消息
        Thread receiverThread = new Thread(() -> {
            try (BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));) {

                String msgFromServer;
                boolean usernameSet = false; // 标记用户名是否已经设置成功
                
                while ((msgFromServer = reader.readLine()) != null) {
                    // 处理用户名相关消息
                    if (msgFromServer.equals("请输入您的用户名:")) {
                        System.out.println(msgFromServer);
                        continue;
                    } else if (msgFromServer.contains("用户名 '") && msgFromServer.contains("' 已存在")) {
                        System.out.println(msgFromServer);
                        continue;
                    } else if (msgFromServer.contains("用户名不能为空")) {
                        System.out.println(msgFromServer);
                        continue;
                    } else if (msgFromServer.startsWith("欢迎 ") && msgFromServer.endsWith(" 加入聊天室!")) {
                        System.out.println(msgFromServer);
                        usernameSet = true;
                        System.out.println("现在您可以开始聊天了！输入 /help 查看可用命令。");
                        continue;
                    } else if (msgFromServer.equals(HeartBeatMessage.HEARTBEAT_REQUEST)) {
                        // 心跳响应
                        mainWriter.println(HeartBeatMessage.HEARTBEAT_RESPONSE);
                        continue;
                    } else {
                        System.out.println(msgFromServer);
                    }
                }

            } catch (IOException e) {
                System.err.println("接收消息时发生异常: " + e.getMessage());
            }
        });
        receiverThread.start();

        try (
                Scanner scanner = new Scanner(System.in)
        ) {
            // 等待用户名设置完成
            while (true) {
                String msg = scanner.nextLine();
                if (msg.equalsIgnoreCase("/quit")) {
                    mainWriter.println("/quit");
                    break;
                }
                mainWriter.println(msg);
            }
        } finally {
            try {
                mainWriter.close();
                if (!socket.isClosed()) {
                    socket.close();
                }
            } catch (IOException e) {
                System.err.println("关闭 socket 时出错: " + e.getMessage());
            }
        }
    }
}